<?php
/**
 * Created by PhpStorm.
 * User:雨寒
 * Date: 2018/8/28
 * Time: 11:20
 */

namespace app\www\controller;

use think\Session;
use think\Db;
use app\common\model\Settings;
class User extends Common
{
    //wap User/index 我的
    public function index(){
        header("Content-type:text/html;charset=utf-8;");
        $access_token=Session::get("access_token");

        $data=array(
            "module"=>'Bussiness',
            "interface"=>'1004',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));

        $result_code=json_decode($res,true)['result_code'];

        $result_info=json_decode($res,true)['result_info'];

        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }

        $result_data=json_decode($res,true)['result_data'];

        $this->assign('info',$result_data['info']);
        return $this->fetch();
    }

    /**
     *关于我们
     */
    public function about(){
        $access_token=Session::get("access_token");

        $data=array(
            "module"=>'Utils',
            "interface"=>'1002',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));

        $result_code=json_decode($res,true)['result_code'];

        $result_info=json_decode($res,true)['result_info'];

        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }

        $result_data=json_decode($res,true)['result_data'];
        $this->assign('about_us',$result_data['about_us']);

        return $this->fetch();
    }

    /**
     *我的活动
     */
    public function myactive(){
        $page_size=5;
        $type = request()->param('type');
        $type = !in_array($type,[1,2]) ? 1 : $type;
        $access_token=Session::get("access_token");

        if(request()->isGet()){

            $this->assign('type',$type);

            $data=array(
                "module"=>'Active',
                "interface"=>'1005',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "type"=>$type,
                    "page"=>1,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('list',$result_data['list']);
            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Active',
                "interface"=>'1005',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "type"=>$type,
                    "page"=>$page,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }


    /**
     *我的消费记录
     */
    public function myorder(){
        $page_size=5;
        $type = request()->param('type');
        $type = !in_array($type,[1,2]) ? 1 : $type;

        $access_token=Session::get("access_token");

        if(request()->isGet()){

            $this->assign('type',$type);

            $data=array(
                "module"=>'Order',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "is_evaluate"=>$type,
                    "page"=>1,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('list',$result_data['list']);

            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Order',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "is_evaluate"=>$type,
                    "page"=>$page,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }

    }

    /**
     *订单提交评价
     */
    public function ordercomm(){
        $access_token = Session::get('access_token');
        $id = request()->param('id');
        if(request()->isGet()){
            $this->assign('id',$id);
            return $this->fetch();
        }
        if(request()->isAjax()){

            $star = request()->param('star');
            $content = request()->param('content');
            $data1=array(
                "module"=>'Order',
                "interface"=>'1003',
                "version"=>'1.0.0',
                "data"=> [
                    "content"=> $content,
                    "order_id"=>$id,
                    "star"=>$star,
                    "access_token"=> $access_token
                ]
            );
            $res=curl_post(config('self_url'),json_encode($data1));
            echo $res;
        }
    }

    /*
     * 合约互通
     *
     * */
    public function agreementList()
    {
        $access_token = Session::get('access_token');
        $type = input('type');
        $type=empty($type)?1:$type;
        $data=array(
            "module"=>'Bussiness',
            "interface"=>'1007',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token,
                "type"=>$type
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));

        $result_code=json_decode($res,true)['result_code'];

        $result_info=json_decode($res,true)['result_info'];

        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }

        $result_data=json_decode($res,true)['result_data'];

        $this->assign('list',$result_data['list']);
        $this->assign('type',$type);
        return $this->fetch();
    }
    /*通过合约*/
    public function passAgreement()
    {
        $id=input('id');
        $res=Db::name('bussiness_agreement')
            ->where('id',$id)
            ->update(array(
                'status'=>1,
                'update_time'=>time()
            ));
        if($res)
        {
            $this->ajaxSuccess("操作成功！");
        }else{
            $this->ajaxError("操作失败！");
        }
    }

    /*
     * 会员激活
     * */
    public function memberActivity()
    {
        $rate=Settings::getSystem();
        $is_app=input('is_app');
        if(request()->isPost())
        {
            $pay_type=input('pay_type');
            $activate_fee=input('activate_fee');
            $access_token=session::get('access_token');
            $data=array(
                "module"=>'Member',
                "interface"=>'1014',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "activate_fee"=>$activate_fee,
                    "pay_type"=>$pay_type
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];
            if($result_code=="0000")
            {
                $result_data=json_decode($res,true)['result_data'];
                $this->ajaxSuccess(json_encode($result_data));
            }else{
                $this->ajaxError($result_info);
            }

        }else{
            $this->assign("activate_fee",$rate['activate_fee']);
            $this->assign("is_app",$is_app);
            return $this->fetch();
        }

    }

    /**
     *我是联盟商
     **/
    public function isUnion(){
        header("Content-type:text/html;charset=utf-8;");
        $access_token=Session::get("access_token");

        $data=array(
            "module"=>'Union',
            "interface"=>'1006',
            "version"=>'1.0.0',
            "data"=>array(
                "access_token"=>$access_token
            )
        );
        $res=curl_post(config('self_url'),json_encode($data));

        $result_code=json_decode($res,true)['result_code'];

        $result_info=json_decode($res,true)['result_info'];

        if($result_code!=="0000")
        {
            if($result_code=="1111")
            {
                $this->redirect('Login/login');
            }
            $this->error($result_info);
        }

        $result_data=json_decode($res,true)['result_data'];
        $this->assign('money',$result_data['money']);
        return $this->fetch();
    }

    /**
     *联盟商订单
     **/
    public function unionOrder(){
        $page_size=5;
        $access_token=Session::get("access_token");

        if(request()->isGet()){
            $data=array(
                "module"=>'Order',
                "interface"=>'1002',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>1,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('list',$result_data['list']);
            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Order',
                "interface"=>'1002',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>$page,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *提现
     **/
    public function withdraw(){
        header("Content-type:text/html;charset=utf-8;");
        $access_token=Session::get("access_token");
        if(request()->isGet()){
            $data=array(
                "module"=>'Union',
                "interface"=>'1006',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('money',$result_data['money']);
            return $this->fetch();
        }

        if(request()->isAjax()){
            $type = request()->param('type');
            $account = request()->param('account');
            $name = request()->param('name');
            $money = request()->param('money');

            $data1=array(
                "module"=>'Withdraw',
                "interface"=>'1001',
                "version"=>'1.0.0',
                "data"=> [
                    "type"=> $type,
                    "account"=>$account,
                    "name"=>$name,
                    "money"=>$money,
                    "access_token"=> $access_token
                ]
            );
            $res=curl_post(config('self_url'),json_encode($data1));
            echo $res;

        }
    }

    /**
     *提现记录
     **/
    public function withdrawList(){
        $page_size=20;
        $access_token=Session::get("access_token");

        if(request()->isGet()){
            $data=array(
                "module"=>'Withdraw',
                "interface"=>'1002',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>1,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));

            $result_code=json_decode($res,true)['result_code'];

            $result_info=json_decode($res,true)['result_info'];

            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                $this->error($result_info);
            }

            $result_data=json_decode($res,true)['result_data'];
            $this->assign('list',$result_data['list']);
            return $this->fetch();
        }
        if(request()->isAjax()){
            $page = request()->param('page');
            $data=array(
                "module"=>'Withdraw',
                "interface"=>'1002',
                "version"=>'1.0.0',
                "data"=>array(
                    "access_token"=>$access_token,
                    "page"=>$page,
                    "page_size"=>$page_size
                )
            );
            $res=curl_post(config('self_url'),json_encode($data));
            echo $res;
        }
    }

    /**
     *影响力中心
     */
    public function effect(){
        $data=input();
        $page=empty($data['page'])?1:input('page');
        $page_size=empty($data['page_size'])?10:input('page_size');

        $access_token=Session::get("access_token");
        $data1=array(
            "module"=>'Member',
            "interface"=>'1015',
            "version"=>'1.0.0',
            "data"=> [
                "page_size"=> $page_size,
                "page"=> $page,
                "access_token"=> $access_token
            ]
        );
        $res=curl_post(config('self_url'),json_encode($data1));

        $result_code=json_decode($res,true)['result_code'];
        $result_info=json_decode($res,true)['result_info'];
        if(request()->isAjax())
        {
            if($result_code!=="0000")
            {
                return 0;
            }else {
                $result_data = json_decode($res, true)['result_data']['list'];
                return json_encode($result_data);
            }
        }else{
            if($result_code!=="0000")
            {
                if($result_code=="1111")
                {
                    $this->redirect('Login/login');
                }
                return $this->error($result_info);

            }else{
                $result_info=json_decode($res,true)['result_data'];
                $this->assign("list",$result_info['list']);
                return $this->fetch();
            }
        }
    }
}